package com.atguigu.realtime.util;

import com.atguigu.realtime.bean.OrderInfo;
import com.atguigu.realtime.commont.Constant;
import org.apache.commons.beanutils.BeanUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/11/24 10:16
 */
public class JdbcUtil {
    private static Connection getMysqlConnection(String mysqlUrl) {
        return getJdbcConnection("com.mysql.cj.jdbc.Driver", mysqlUrl);
    }
    
    public static Connection getPhoenixConnection(String phoenixUrl) {
        String phoenixDriver = Constant.PHOENIX_DRIVER;
        return getJdbcConnection(phoenixDriver, phoenixUrl);
    }
    
    private static Connection getJdbcConnection(String driver, String url) {
        try {
            Class.forName(driver);  // 加载驱动
            return DriverManager.getConnection(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static void main(String[] args) {
//        Connection conn = getPhoenixConnection(Constant.PHOENIX_URL);
        Connection conn = getMysqlConnection("jdbc:mysql://hadoop162:3306/gmall2021?user=root&password=aaaaaa");
        List<OrderInfo> list = queryList(conn, "select * from order_info", new Object[]{}, OrderInfo.class);
        for (OrderInfo obj : list) {
            
            System.out.println(obj);
        }
    }
    
   
    
    // 通过jdbc去数据查询数据:
    public static <T> List<T> queryList(Connection conn, String querySql, Object[] args, Class<T> tClass) {
        
        ArrayList<T> result = new ArrayList<>();
        
        PreparedStatement ps = null;
        
        try {
            ps = conn.prepareStatement(querySql);
            // 给占位符赋值
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            /*
            name   id   age
            lisi    1   10
            zs      2   20
            ww      3   15
             */
            ResultSet resultSet = ps.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData(); // 获取结果的元数据
            int columnCount = metaData.getColumnCount();
            // 遍历sql查询到的结果集, 每一行的数据封装到一个T类型的对象中
            while (resultSet.next()) {
                // 先创建一个T的对象
                T t = tClass.newInstance();  // new T()  调用T的无参构造器来创建对象
                // 获取每一列: 1. 有几列? 2. 获取列名  3. 获取列值
                for (int i = 1; i <= columnCount; i++) {
                    // 获取列名. 将来就是T的字段名
                    String columnName = metaData.getColumnName(i);
                    Object columnValue = resultSet.getObject(columnName);
                    BeanUtils.setProperty(t, columnName, columnValue);  //给t对象的 columnName属性 赋值: columnValue
                }
                result.add(t);
                
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ps != null) {
                
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        return result;
    }
    
}
